"""
author: liuxu
date: 2025/4/19
description: 增加系统级性能分析工具--libc-trace测试用例
"""

import allure
import pytest
import re
from common.logger_ctrl import mylogger
from common.ssh_command import exec_cmd
from base_public.testbase import BasePublic

@allure.feature('libc tracing指令')
@pytest.mark.medium
class TestLibcTrace(BasePublic):
    """测试libc trace功能"""
    @pytest.mark.smoke
    @pytest.mark.high
    def test_libc_trace(self, test_env):
        """
        测试hipprof --libc-trace
        :return:
        """
        testcase = self.compile_source_code(src_file_name="hipSimple/matrix.cpp")
        ret, output = exec_cmd(f"hipprof --libc-trace {testcase}", cwd=test_env)
        assert ret is True
        # 获取生成的libc的csv文件，读取csv的trace数据
        libc_csv_path = self.get_all_prof_files_info(output, test_env)["libc_api_csv"]["abspath"]
        libc_trace_d = self.get_trace_info_from_csv(libc_csv_path)
        # 断言HIP PROF:libc API statistics有生成libc结果，且程序总运行时间小于1s(10的9次方纳秒)
        assert len(libc_trace_d)>0 and libc_trace_d["Total"]["TotalDurationNs"] < 1 * (10**9)

    def test_libc_trace_level(self, test_env):
        """
        测试hipprof --libc-trace-level (0:跟踪常用libc函数；1-跟踪所有libc接口（默认为1）)
        :return:
        """
        testcase = self.compile_source_code(src_file_name="hipSimple/matrix.cpp")
        ret, output = exec_cmd(f"hipprof --libc-trace --libc-trace-level 0 {testcase}", cwd=test_env)
        assert ret is True
        # 获取生成的libc的csv文件，读取csv的trace数据
        libc_csv_path = self.get_all_prof_files_info(output, test_env)["libc_api_csv"]["abspath"]
        libc_trace_d = self.get_trace_info_from_csv(libc_csv_path)
        # 断言HIP PROF:libc API statistics有生成libc结果，且程序总运行时间小于3s(10的9次方纳秒)
        assert len(libc_trace_d)>0 and libc_trace_d["Total"]["TotalDurationNs"] < 3 * (10**9)

    def test_libc_multi_process(self, test_env):
        """
        测试多卡多进程多线程用例
        :return:
        """
        testcase = self.compile_source_code(src_file_name="multiproc_case/multiprocess_multithread_multidevice_test.cpp")
        ret, output = exec_cmd(f"hipprof --libc-trace {testcase}", cwd=test_env)
        assert ret is True
        # 获取生成的libc的csv文件，读取csv的trace数据
        libc_csv_path = self.get_all_prof_files_info(output, test_env)["libc_api_csv"]["abspath"]
        libc_trace_d = self.get_trace_info_from_csv(libc_csv_path)
        assert len(libc_trace_d)>0

    @pytest.mark.skip(reason="This parameter is no longer supported after dtk-25.04.2")
    def test_libc_hid_args(self, test_env):
        """
        测试不收集函数参数
        :return:
        """
        testcase = self.compile_source_code(src_file_name="hipSimple/matrix.cpp")
        ret, output = exec_cmd(f"hipprof --libc-trace --hid-args {testcase}", cwd=test_env)
        assert ret is True
        # 获取生成的libc的csv文件，读取csv的trace数据
        libc_csv_path = self.get_all_prof_files_info(output, test_env)["libc_api_csv"]["abspath"]
        libc_trace_d = self.get_trace_info_from_csv(libc_csv_path)
        for info_dict in libc_trace_d:
            if "name" in info_dict and "args" in info_dict:
                assert info_dict["args"].get("args", "") == "", f'{info_dict["name"]}函数的参数不为空'

    def test_libc_buffer_size(self, test_env):
        """
        设置数据刷新频率
        :return:
        """
        testcase = self.compile_source_code(src_file_name="hipSimple/matrix.cpp")
        ret, output = exec_cmd(f"hipprof --libc-trace --buffer-size 1 {testcase}", cwd=test_env)
        assert ret is True
        # 获取生成的libc的csv文件，读取csv的trace数据
        libc_csv_path = self.get_all_prof_files_info(output, test_env)["libc_api_csv"]["abspath"]
        libc_trace_d = self.get_trace_info_from_csv(libc_csv_path)
        # 断言HIP PROF:libc API statistics有生成libc结果，且程序总运行时间小于1s(10的9次方纳秒)
        assert len(libc_trace_d)>0 and libc_trace_d["Total"]["TotalDurationNs"] < 1 * (10**9)

    def test_libc_index_range(self, test_env):
        """
        按执行顺序过滤api数据
        :return:
        """
        testcase = self.compile_source_code(src_file_name="hipSimple/matrix.cpp")
        ret, output = exec_cmd(f"hipprof --libc-trace --hip-trace --index-range 2:3 {testcase}", cwd=test_env)
        assert ret is True
        # 获取生成的libc的csv文件，读取csv的trace数据
        libc_csv_path = self.get_all_prof_files_info(output, test_env)["libc_api_csv"]["abspath"]
        libc_trace_d = self.get_trace_info_from_csv(libc_csv_path)
        assert len(libc_trace_d)>0

    def test_libc_omp_case(self, test_env):
        """
        测试omp程序
        :return:
        """
        ompcase = self.compile_omp_case()
        ret, output = exec_cmd(f"hipprof --libc-trace {ompcase}", cwd=test_env)
        assert ret is True
        # 获取生成的libc的csv文件，读取csv的trace数据
        libc_csv_path = self.get_all_prof_files_info(output, test_env)["libc_api_csv"]["abspath"]
        libc_trace_d = self.get_trace_info_from_csv(libc_csv_path)
        assert len(libc_trace_d) > 0

    def test_libc_python(self, test_env):
        """
        测试omp程序
        :return:
        """
        ret, output = exec_cmd(f"hipprof --libc-trace python3 -c 'print(1+1)'", cwd=test_env)
        assert ret is True
        # 获取生成的libc的csv文件，读取csv的trace数据
        libc_csv_path = self.get_all_prof_files_info(output, test_env)["libc_api_csv"]["abspath"]
        libc_trace_d = self.get_trace_info_from_csv(libc_csv_path)
        assert len(libc_trace_d) > 0

    #TODO 增加测试opencl用例